package com.download.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.download.backstage.entity.User;
import com.download.web.entity.SysUser;
import com.download.web.entity.model.Page;
import com.download.web.mapper.SysUserMapper;
import com.download.web.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cmy
 * @since 2019-02-20
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    private SysUserMapper userMapper;
    @Override
    public Page<User> userPage(Integer pageIndex, Integer pageSize) {
        List<User> users=userMapper.userList(pageIndex*pageSize,pageSize);
        return new Page<User>(users,userMapper.userCount(),pageIndex,pageSize);
    }

    @Override
    public SysUser login(String email, String pwd) {
        try {
            SysUser user=userMapper.selectOne(new QueryWrapper<SysUser>().eq("email",email));
            if(user!=null){
                if(user.getPwd().equals(pwd)){
                    return user;
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Integer register(SysUser user) {
        try {
            int count=userMapper.insert(user);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void updateUserName(String userName, Integer id) {
        userMapper.updateUserName(userName,id);
    }

    @Override
    public void updateUserImg(String headImg, Integer id) {
        userMapper.updateHeadImg(headImg,id);
    }

    @Override
    public void updatePwd(String pwd, Integer id) {
        userMapper.updatePwd(pwd,id);
    }
}
